<?php

class xinnghiphep extends CI_Controller{  
    public $mahoso;
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('userlayout');
        $this->userlayout->setLayout("user/layout");
        $userSession = $this->session->userdata('user');
        $this-> view_data['base_url'] = base_url();
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }
        $this->mahoso = $userSession['MaHoSo'];
    }
    
    function index(){
        $this -> DangKyNghiPhepAction();
    }
    
    
    function DangKyNghiPhepAction(){
        
        
        $this->load->library('form_validation');
		
		$this->form_validation->set_rules('startday', 'Startday', 'trim|required|xss_clean');
       	$this->form_validation->set_rules('numberday', 'Numberday', 'trim|required|xss_clean');
        $this->form_validation->set_rules('why', 'Why', 'trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{
            $currTemp = array('&nbsp;&nbsp;Xin Nghỉ Phép');
            $current['current'] = $currTemp;
            $title = 'Xin Nghỉ Phép';
            $this->userlayout->view("user/XinNghiPhepFormView",$this-> view_data ,$title, $current);
		}
		else
		{
			$username = $this->input->post('username');
            $startday = $this->input->post('startday');
            $numberday = $this->input->post('numberday');
            $why = $this->input->post('why');
			$MaHoSo = $this->mahoso;
			if($MaHoSo != 0)
			{
				$data = array(
                            'NgayBatDauNghi' => $startday,
                            'SoNgayNghi' => $numberday,
                            'LyDoNghi' => $why,
                            'TrangThaiXacNhan' => '0',
                            'MaHoSo' => $MaHoSo,
                            );
				$this->load->database();
				$this->db->insert("ngaynghi",$data);
				
				$title = 'Xin Nghỉ Phép';
				$this->view_data['data'] = $data;
                $currTemp = array('&nbsp;&nbsp;Xin nghỉ phép thành công');
                $current['current'] = $currTemp;
				$this->userlayout->view("user/DangKiNghiPhepThanhCong",$this-> view_data ,$title, $current);
			}
			else
			{
                $currTemp = array('&nbsp;&nbsp;Xin nghỉ phép thất bại');
                $current['current'] = $currTemp;
				$title = 'Xin Nghỉ Phép';
				$this->userlayout->view("user/DangKiNghiPhepThatBai",$this-> view_data ,$title);
			}                  		
		}
        
    }
	
	function LayMaHoSo($TenNhanVien)
	{
		$cmdquery = "Select MaHoSo From hosonhanvien Where HoTen = '" . $TenNhanVien. "';";
		$this->load->database();
		$query = $this->db->query($cmdquery);
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row->MaHoSo;			
		}
		return 0;		
	}
     
}

?>